<template>
  <div class="materialReturnBill">
    <div class="publicSearch">
      <div class="searchBar" :style="{ height: isMoreSearch ? 'auto' : '' }">
        <el-button-group>
          <el-button @click="getmaterialReturnBillData" size="mini" icon="el-icon-search">查询</el-button>
          <el-button @click="clearSearchData" size="mini" icon="el-icon-refresh">重置</el-button>
        </el-button-group>
        <div>
          <span>入库单明细号</span>
          <el-input v-model="searchData.asnBillNo" size="mini" placeholder="入库单明细号" clearable></el-input>
        </div>
        <!-- <div>
          <span>采购单明细号</span>
          <el-input
            v-model="searchData.requestDetailDetailNo"
            size="mini"
            placeholder="采购单明细号"
            clearable
          ></el-input>
        </div>-->
        <div>
          <span>物料名称</span>
          <el-input v-model="searchData.itemName" size="mini" placeholder="物料名称" clearable></el-input>
        </div>
        <div>
          <span>物料编码</span>
          <el-input v-model="searchData.itemCode" size="mini" placeholder="物料编码" clearable></el-input>
        </div>
        <div>
          <span>关键字搜索</span>
          <el-input
              v-model="searchData.keyWords"
              size="mini"
              title="批次名称/验收人/追踪号/货位编码/包装"
              placeholder="批次名称/验收人/追踪号/货位编码/包装"
              clearable
          ></el-input>
        </div>
      </div>
    </div>

    <div class="headerBtns">
      <el-button size="small" type="primary" @click="confirm">确认</el-button>
    </div>

    <div class="tableBox">
      <el-table
          v-loading="loading.masterTable"
          highlight-current-row
          @selection-change="handleSelectionChange"
          :data="materialReturnBills"
          style="width: 100%"
      >
        <el-table-column fixed="left" type="index" width="50"></el-table-column>
        <el-table-column type="selection" width="55"></el-table-column>
        <!-- <el-table-column
          sortable
          :v-show="false"
          prop="qcRecordId"
          label="qcRecordId"
          width="150"
        ></el-table-column> -->
        <el-table-column
            sortable
            show-overflow-tooltip
            prop="billNo"
            label="入库任务明细"
            width="160"
        >
          <!-- <template slot-scope="scope">
            <span
              class="textBtn"
              @click="
                $router.push({
                  name: 'asnBillDetail',
                  params: { billNo: scope.row.asnDetailCode },
                })
              "
            >{{ scope.row.asnDetailNo }}</span>
          </template> -->
        </el-table-column>
        <!-- <el-table-column
          sortable
          show-overflow-tooltip
          prop="asnMasterCode"
          label="入库任务号"
          width="150"
        ></el-table-column> -->
        <el-table-column
            sortable
            show-overflow-tooltip
            prop="acceptRecordCode"
            label="收货单号"
            width="180"
        ></el-table-column>
        <el-table-column sortable show-overflow-tooltip prop="returnState" label="状态" width="100">
          <template slot-scope="scope">
            <el-tag

                v-if="scope.row.returnState === 0"
                type="info"
            >初始化
            </el-tag>
            <el-tag

                v-if="scope.row.returnState === 1"
                type="success"
            >已确认
            </el-tag>
          </template>
        </el-table-column>

        <!-- <el-table-column
          sortable
          show-overflow-tooltip
          prop="organizationName"
          label="货主"
          width="120"
        >-->
        <!-- <template slot-scope="scope">
            <span
              class="textBtn"
              @click="
                $EventBus.$emit(
                  'showPublicModal',
                  scope.row.organizationId,
                  'organization'
                )
              "
            >{{ scope.row.organizationName }}</span>
        </template>-->
        <!-- </el-table-column> -->
        <el-table-column sortable show-overflow-tooltip prop="itemName" label="物料名称" width="160">
          <template slot-scope="scope">
            <span
                class="textBtn"
                @click="
                $EventBus.$emit('showPublicModal', scope.row.itemCode, 'item')
              "
            >{{ scope.row.itemName }}</span>
          </template>
        </el-table-column>
        <el-table-column sortable show-overflow-tooltip prop="itemCode" label="物料编码" width="160"></el-table-column>
        <el-table-column sortable show-overflow-tooltip prop="batchName" label="批次" width="160"></el-table-column>
        <el-table-column show-overflow-tooltip prop="fiedQuantity" label="不合格数量" width="120"></el-table-column>
        <el-table-column show-overflow-tooltip prop="concessionQuantity" label="让步数量" width="120"></el-table-column>
        <el-table-column show-overflow-tooltip prop="supplierName" label="供应商" width="120"></el-table-column>

        <!-- <el-table-column sortable show-overflow-tooltip prop="cellCode" label="货位编码" width="120"></el-table-column> -->
        <!-- <el-table-column sortable show-overflow-tooltip prop="boxCode" label="追踪号" width="120"></el-table-column> -->
        <el-table-column sortable show-overflow-tooltip prop="qcUserName" label="质检员" width="100">
          <template slot-scope="scope">
            <span
                class="textBtn"
                @click="
                $EventBus.$emit(
                  'showPublicModal',
                  scope.row.acceptUserId,
                  'account'
                )
              "
                type="text"
            >{{ scope.row.qcUserName }}</span>
          </template>
        </el-table-column>
        <el-table-column sortable show-overflow-tooltip prop="qcTime" label="质检时间" width="180">
          <template slot-scope="scope">{{ scope.row.qcTime.substr(2, 17) }}</template>
        </el-table-column>

        <el-table-column
            sortable
            show-overflow-tooltip
            label="操作"

        >
          <template slot-scope="scope">
            <el-dropdown
                style="margin-left: 10px; margin-right: 10px"
                size="medium"
                type="primary"
                icon="iconfont icon-print"
                @click="demoPrint(scope.row.qcRecordCode)"
                split-button
            >
              打印
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item
                ><el-button
                    size="small"
                    type="primary"
                    @click="$EventBus.$emit('setPrinter')"
                >打印设置</el-button
                ></el-dropdown-item
                >
                <el-dropdown-item
                ><el-button
                    style="margin-top: 2px"
                    size="small"
                    type="primary"
                    @click="demoPrint(scope.row.qcRecordCode, 1)"
                >打印预览1</el-button
                ></el-dropdown-item
                >

                <el-dropdown-item
                ><el-button
                    style="margin-top: 2px"
                    size="small"
                    type="primary"
                    @click="demoPrint(scope.row.qcRecordCode, 2)"
                >打印预览2</el-button
                ></el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
        <!--        <el-table-column fixed="right" label="操作" min-width="80">-->
        <!--          <template slot-scope="scope">-->
        <!--            <el-button-->
        <!--              @click="showNewModal(scope.row)"-->
        <!--              type="primary"-->
        <!--              size="small"-->
        <!--              :disabled="scope.row.returnState === 1"-->
        <!--            >&nbsp;让步</el-button>-->
        <!--          </template>-->
        <!--        </el-table-column>-->
      </el-table>
      <el-pagination
          class="pagination-bottom"
          style="text-align: right"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="searchData.pageNum"
          :page-sizes="[15, 30, 50, 100]"
          :page-size="searchData.pageSize"
          layout="sizes,total, prev, pager, next"
          :total="searchData.total"
      ></el-pagination>
    </div>

    <el-dialog title="让步" :visible.sync="checkQc" :close-on-click-modal="false" width="20%" center>
      <el-form :model="form" :rules="rules" ref="form">
        <el-form-item label="不合格数量" prop="fiedQuantity" :label-width="formLabelWidth">
          <span>{{form.fiedQuantity}}</span>
        </el-form-item>
        <el-form-item label="让步数量" prop="concessionQuantity" :label-width="formLabelWidth">
          <el-input
              type="number"
              v-model="form.concessionQuantity"
              placeholder="让步数量"
              maxlength="20"
              style="width: 90%;"
          ></el-input>
        </el-form-item>

        <!-- <el-radio-group v-model="form.qcResult">
          <el-radio :label="0">不合格</el-radio>
          <el-radio :label="1">合格</el-radio>
          <el-radio :label="2">让步</el-radio>
        </el-radio-group>-->
      </el-form>

      <span slot="footer" class="dialog-footer">
        <el-button @click="checkQc = false" size="mini">取 消</el-button>
        <el-button type="primary" @click="ChangeQc" size="mini">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "materialReturnBill",
  data() {
    // 校验规则
    return {
      isMoreSearch: false,
      loading: {
        masterTable: true
      },
      // 用作查询的数据
      searchData: {
        pageNum: 1,
        pageSize: 15,
        total: null,
        keyWords: "",
        wareId: PF.getLocal("wareId", "number"),
        itemCode: "",
        itemName: "",
        organizationId: null,
        asnDetailNo: "",
        requestDetailDetailNo: "",
        billType: ""
      },
      searchStates: [
        {
          value: "enable",
          label: "启用"
        },
        {
          value: "disable",
          label: "禁用"
        }
      ],
      //存放装箱单数据的数组
      materialReturnBills: [],
      form: {
        // 仓库编码 获得当前仓库的编码
        wareId: PF.getLocal("wareId", "number"),
        // 不合格数量
        fiedQuantity: "",
        //  让步数量
        concessionQuantity: "0"
      },
      rules: {
        concessionQuantity: [
          { required: true, message: "请输入让步数量", trigger: "blur" }
        ]
      },
      formLabelWidth: "120px",
      rowData: {
        //
      },
      Mystyle: { height: "50px" },
      filterText: "",
      defaultProps: {
        children: "children",
        label: "name"
      },
      roleRowInfo: {
        //
      },
      roleForm: {
        //
      },
      roleRules: {
        //
      },
      tableRowClassName({ row, rowIndex }) {
        if (row.accountStatus === "disable") {
          return "disable-row";
        } else if (row.accountStatus === "enable") {
          return "enable-row";
        }
        return "";
      },
      // 显示检测弹框
      checkQc: false,
      multipleSelection: [],
    };
  },
  computed: {},
  watch: {
    //
  },
  methods: {
    demoPrint: function (billNo, isReview) {
      var that = this;
      IOT.getServerData(
          "/qc/records/allotPrint/",
          "get",
          {billNo:billNo},
          (ret) => {
            if (ret.code === 200) {
              var strHTML = ret.data;

              that.$EventBus.$emit("printHtml", strHTML, isReview);
            } else {
              IOT.tips(ret.message || "服务器请求失败，稍后再试！", "error");
            }
          },
          true
      );
    },

    //分页设置每页的条数
    handleSizeChange(val) {
      this.searchData.pageSize = val;
      this.getmaterialReturnBillData();
    },
    //分页设置从第几条开始
    handleCurrentChange(val) {
      this.searchData.pageNum = val;
      this.getmaterialReturnBillData();
    },
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 清空查询条件
    clearSearchData() {
      this.searchData.keyWords = "";
      this.searchData.itemCode = "";
      this.searchData.itemName = "";
      this.searchData.organizationId = "";
      this.searchData.asnDetailNo = "";
      this.searchData.requestDetailDetailNo = "";
      this.searchData.billType = "";
    },
    //根据筛选条件查询数据
    getmaterialReturnBillData() {
      let that = this;
      that.loading.masterTable = true;
      let searchData = JSON.parse(JSON.stringify(this.searchData));
      IOT.getServerData("/qc/records/return/list", "get", searchData, ret => {
        that.loading.masterTable = false;
        if (ret.code === 200) {
          // rows就是返回的数据
          this.materialReturnBills = ret.rows;
          this.searchData.total = ret.total;
          this.searchData.pageNum = ret.pageNumber;
        } else {
          IOT.tips(ret.message || "服务器请求失败，稍后再试！", "error");
        }
      });
    },

    showNewModal(row) {
      if (row == null) {
        this.$message({
          message: "请选择一条数据",
          type: "warning"
        });
      } else {
        this.checkQc = true;
        this.form.qcRecordId = row.qcRecordId;
        this.form.fiedQuantity = row.fiedQuantity;
        this.form.concessionQuantity = row.concessionQuantity;
      }
    },

    // 提交质检结果
    ChangeQc() {
      // 关闭弹框
      this.checkQc = false;
      var params = {
        billCode: "",
        qcRecordList: [
          {
            qcRecordId: this.form.qcRecordId,
            concessionQuantity: this.form.concessionQuantity
          }
        ]
      };
      console.log(JSON.stringify(params));
      // 后台保存质检结果
      IOT.getServerData("/qc/records/concession", "post", params, ret => {
        if (ret.code === 200) {
          IOT.tips("保存成功！", "success");
          this.getmaterialReturnBillData();
        } else {
          IOT.tips(ret.message || "服务器请求失败，稍后再试！", "error");
        }
      });
    },
    confirm() {
      let data = this.multipleSelection;
      let qcRecordList = [];
      data.forEach(item => {
        qcRecordList.push({
          qcRecordId:item.qcRecordId
        });
      });
      console.log(PF.JSON(qcRecordList));
      var params = {
        billCode: "",
        qcRecordList: qcRecordList
      };
      IOT.getServerData("/qc/records/return/confirm", "post", params, ret => {
        if (ret.code === 200) {
          IOT.tips("保存成功！", "success");
          this.getmaterialReturnBillData();
        } else {
          IOT.tips(ret.message || "服务器请求失败，稍后再试！", "error");
        }
      });
    }
  },
  // 页面加载时刷新
  created() {
    this.getmaterialReturnBillData();
  }
};
</script>

<style scoped lang="less">
.materialReturnBill {
}
</style>
